package com.bckj.fastboot.log.impl;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.log.LogProducer;
import com.bckj.fastboot.log.constant.LogConstant;
import com.bckj.fastboot.log.model.LogRecord;
import com.bckj.fastboot.log.model.LogRecordMessage;
import com.bckj.fastboot.mq.ons.AbstractOnsProducer;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class OnsLogProducer extends AbstractOnsProducer<LogRecordMessage> implements LogProducer {

    private final Producer producer;

    @Override
    public void produce(List<LogRecord> log) {
        sendMessage(new LogRecordMessage(log));
    }

    @Override
    public void sendMessage(LogRecordMessage params) {
        String jsonStr = JsonUtils.toJsonStr(params);
        Message message = new Message(
                LogConstant.TOPIC,
                null,
                jsonStr.getBytes()
        );
        producer.sendAsync(message, new SimpleSendCallback(params));
    }
}
